package org.example.controller;


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.example.pojo.R;
import org.example.pojo.Schedule;
import org.example.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Controller
@EnableWebMvc
@RequestMapping("schedule")
@Slf4j
@ResponseBody
@CrossOrigin //允许其他源访问该资源
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @GetMapping("/{pageSize}/{currentPage}")
    public R page(@PathVariable int pageSize, @PathVariable int currentPage) {


        R r = scheduleService.page(pageSize, currentPage);
        log.info("查询的数据为:{}",r);
        return r;
    }

    @DeleteMapping("/{id}")
    public R remove(@PathVariable int id) {
        R r = scheduleService.remove(id);
        return r;
    }

    @PostMapping
    public R save(@Validated @RequestBody Schedule schedule, BindingResult result){
        if(result.hasErrors()) {
            return R.fail("参数为null，不能报存");
        }
        R r = scheduleService.save(schedule);
        return r;
    }

    @PutMapping
    public R update(@Validated @RequestBody Schedule schedule,BindingResult result) {
        if(result.hasErrors()) {
            return R.fail("参数为null，不能报存");
        }
        R r = scheduleService.update(schedule);
        return r;
    }
}
